
#include "nane/RenderSystem/Texture.h"
#include "nane/RenderSystem/TextureImpl_.h"
#include "nane/RenderSystem/RenderSystemImpl_.h"

namespace nane
{
    ////////////////////////////////////////////////////////////////////////////////////////////////
    Texture::Texture( const NullType& )
        : SubSystemProduct()
        , m( NULL )
    {
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    Texture::Texture( Implementation* _impl )
        : SubSystemProduct( _impl )
        , m( _impl )
    {
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    Texture::Texture( const Texture& _other )
        : SubSystemProduct( _other )
        , m( _other.m )
    {
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    byte* Texture::Lock( uint32 _x, uint32 _y, uint32 _width, uint32 _height, uint32* _pitch )
    {
        NANE_ASSERT_RV( m != NULL, "Texture must be valid", NULL );
        NANE_ASSERT_RV( _x < m->width
                     && _y < m->height
                     && _x + _width <= m->width
                     && _y + _height <= m->height
                     , "Locking rectangle must be within texture dimensions", NULL );
        NANE_ASSERT_RV( _pitch != NULL, "pitch is required parameter", NULL );
        byte* lockedMemory = m->creator->interface->LockTexture(m->interface, _x, _y, _width, _height, _pitch);
        if( lockedMemory != NULL )
        {
            ++m->lockCount;
        }
        return lockedMemory;
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
    void Texture::Unlock()
    {
        NANE_ASSERT_R( m != NULL, "Texture must be valid" );
        if( m->lockCount > 0 )
        {
            m->creator->interface->UnlockTexture(m->interface);
            --m->lockCount;
        }
    }
    ////////////////////////////////////////////////////////////////////////////////////////////////
}   // namespace nane
